package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Kind;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IKindService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import com.alibaba.druid.util.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author 雷德望
 */
@Controller
@RequestMapping("kind")
public class KindController {
    @Autowired
    private IKindService kindService;
    
    @RequiresPermissions("kind:index")
    @PermissionName("分类首页")
    @RequestMapping("")
    public String index(){
        return "kind/list";
    }

    @RequiresPermissions("kind:kindlist")
    @PermissionName("分类数据")
    @RequestMapping("kindList")
    public String kindList(){
        return "kind/kindList";
    }

    @RequiresPermissions("kind:list")
    @PermissionName("分类列表")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryObject qo){
        return kindService.query(qo);
    }
    @RequestMapping("queryKinds")
    @ResponseBody
    public List<Kind> queryKinds(){
        return kindService.queryKinds();
    }

    @RequestMapping("queryDatas")
    @ResponseBody
    public List<Kind> queryDatas(Long parentId){

        return kindService.queryDatas(parentId);
    }


    @RequestMapping("queryKindTwo")
    @ResponseBody
    public List<Map<String ,Object>>  queryKindTwo(){

        return kindService.queryKindTwo();
    }






    @RequiresPermissions("kind:save")
    @PermissionName("分类保存")
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Kind entity ,Long parentId){
        JsonResult result = new JsonResult();
        System.out.println(parentId);
        try {
            if(StringUtils.isEmpty(entity.getName())){
              @SuppressWarnings("unused")
			int i =1/0;
            }

            if (parentId!=null){
                Kind kind = kindService.get(parentId);
                entity.setParent(kind);
            }
            kindService.save(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.markMsg("分类不能为空");
        }
        return result;
    }
    @RequiresPermissions("kind:update")
    @PermissionName("分类更新")
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Kind entity){
        JsonResult result = new JsonResult();
        try {


            kindService.update(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.markMsg("分类更新失败1");
        }
        return result;
    }

    @RequestMapping("delete")
    @ResponseBody
    @RequiresPermissions("kind:delete")
    @PermissionName("角色删除")
    public JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try {
            kindService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("删除操作失败" + e.getMessage());
        }
        return result;
    }
}
